package main

import (
	"time"
	"fmt"
	"sync"
	"runtime"
	"os"
	"strconv"
)

var (
	primaryNums = make(map[int]int)
	lock sync.Mutex
)
func main(){
	//fmt.Println(len(primaryNums))
	n := 99999
	routineCount := runtime.NumCPU()
	step := n/routineCount
	i := 1
	var start int
	var end int
	for {
		start = (i-1) * step + 1
		end = start + step
		if end > n {
			end = n
		}
		//fmt.Printf("start is %d ",start)
		//fmt.Printf("end is %d \n",end)
		primary(start,end)
		if end >= n {
			break
		}
		i++
	}
	time.Sleep(time.Second * 1)
	File,_ := os.OpenFile("./primary.txt",os.O_CREATE|os.O_WRONLY|os.O_TRUNC,0666)
	for i:=0;i<len(primaryNums);i++{
		File.WriteString(strconv.FormatInt(int64(primaryNums[i]),10))
		if i< (len(primaryNums) - 1)  {
			File.WriteString("\n")
		}
		fmt.Printf("%d ",primaryNums[i])
	}
}

func primary(start int,end int) {
	for i:= start;i<=end;i++{
		if isPrimary(i) {
			primaryNums[len(primaryNums)] = i
		}
	}
}

func isPrimary(n int) bool{
	var isPrimary = true
	if n == 1 {
		return false
	}
	if n == 2 {
		return true
	}
	if n % 2 == 0 {
		return false
	}
	for i:=3;i*i<=n;i+=2{
		if n % i == 0{
			isPrimary = false
		}
	}
	return isPrimary
}